﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TSSControlLibrary
{
    public partial class CronHourControl : UserControl
    {
        /// <summary>
        /// 回调函数  委托类型
        /// </summary>
        /// <param name="expression"></param>
        public delegate void Vaction(string expression, Control c);
        public Vaction returnExpression;
        /// <summary>
        /// 保存表达式信息
        /// </summary>
        private string ExPression = null;
        private bool endInitializeFlag;
        /// <summary>
        /// 控件构造函数
        /// </summary>
        public CronHourControl()
        {
            InitializeComponent();
            InitializeControl();
            endInitializeFlag = true;
        }
        /// <summary>
        /// 初始化控件数据
        /// </summary>
        private void InitializeControl()
        {
            this.numericUpDown_wk1.Value = 1;
            this.numericUpDown_wk2.Value = 2;
            this.numericUpDown_xh1.Value = 0;
            this.numericUpDown_xh2.Value = 1;
            this.radioButton_hour.Checked = true;
            foreach (var control in this.Controls)
            {
                if (control is CheckBox)
                {
                    CheckBox cb = control as CheckBox;
                    cb.CheckedChanged += checkBoxCheckedChanged;
                }
            }
        }
        /// <summary>
        /// radiobutton 按钮点击选中事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton_hour.Checked)
            {
                this.ExPression = "*";
            }
            else if (radioButton_wk.Checked)
            {
                this.ExPression = this.numericUpDown_wk1.Value + "-" + this.numericUpDown_wk2.Value;
            }
            else if (radioButton_xh.Checked)
            {
                this.ExPression = this.numericUpDown_xh1.Value + "/" + this.numericUpDown_xh2.Value;
            }
            else if (radioButton_zd.Checked)
            {
                ExPression = null;
                this.checkBox_00.Checked = true;
                foreach (var control in this.Controls)
                {
                    if (control is CheckBox)
                    {
                        CheckBox cb = control as CheckBox;
                        if (cb.Checked)
                        {
                            if (ExPression == null||ExPression == "")
                            {
                                int m = Convert.ToInt32(cb.Text);
                                this.ExPression = m.ToString();
                            }
                            else
                            {
                                int m = Convert.ToInt32(cb.Text);
                                this.ExPression = m.ToString() + "," + ExPression;
                            }
                        }
                    }
                }
            }
            //调用回调方法  返回表达式
            if (endInitializeFlag)
            {
                returnExpression(ExPression, this);
            }
        }
        /// <summary>
        /// 数字下拉框值改变时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDown_ValueChanged(object sender, EventArgs e)
        {
            NumericUpDown n = sender as NumericUpDown;
            if (n.Name == "numericUpDown_wk1" || n.Name == "numericUpDown_wk2")
            {
                this.radioButton_wk.Checked = true;
                this.ExPression = this.numericUpDown_wk1.Value + "-" + this.numericUpDown_wk2.Value;
            }
            else if (n.Name == "numericUpDown_xh1" || n.Name == "numericUpDown_xh2")
            {
                this.radioButton_xh.Checked = true;
                this.ExPression = this.numericUpDown_xh1.Value + "/" + this.numericUpDown_xh2.Value;
            }
            //调用回调方法  返回表达式
            if (endInitializeFlag)
            {
                returnExpression(ExPression, this);
            }
        }
        /// <summary>
        /// checkbox选中时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBoxCheckedChanged(object sender, EventArgs e)
        {
            if (radioButton_zd.Checked)
            {
                CheckBox cb = sender as CheckBox;
                if (cb.Checked)
                {
                    if (ExPression == null || ExPression == "")
                    {
                        int m = Convert.ToInt32(cb.Text);
                        this.ExPression = m.ToString();
                    }
                    else
                    {
                        int m = Convert.ToInt32(cb.Text);
                        this.ExPression += "," + m.ToString();
                    }
                }
                else
                {
                    int m = Convert.ToInt32(cb.Text);
                    if (ExPression.Contains(","))
                    {
                        if (ExPression.StartsWith(m.ToString(), StringComparison.CurrentCulture))
                        {
                            this.ExPression = this.ExPression.Replace(m.ToString() + ",", "");
                        }
                        else
                        {
                            this.ExPression = this.ExPression.Replace("," + m.ToString(), "");
                        }
                    }
                    else
                    {
                        this.ExPression = this.ExPression.Replace(m.ToString(), "");
                    }
                }
                //调用回调方法  返回表达式
                if (endInitializeFlag)
                {
                    returnExpression(ExPression, this);
                }
            }
        }
    }
}
